package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by quxingyu on 2015/8/6.
 */
@SuppressWarnings("serial")
public class Question extends Model<Question> {
    public static final Question dao = new Question();
    /**
     * 随机获取各种类型的题目
     * 注：这种获取随机值的方法在mysql的API中是不建议使用的。效率很低。如果数据量达到几十万上百万时，反应速度非常慢。
     * */
    public List<Question> getQuestions(String type, int num, String categorys) {
       /* if(num==0){
           return Question.dao.find("select a.* from t_question a  where  a.category_id in (?) and a.type=? order by rand()",categorys,type);
        }else{*/
        return Question.dao.find("select a.* from t_question a where a.category_id in (?) and a.type= ? order by rand() limit ?",categorys,type,num);
        /*}*/
    }

    /**
     * <p>通过Id获取试题 </p>
     * 
     * */
    public List<Question> getQuestionByIds(String ids) {
    	if (ids==null || "".equals(ids)) {
    		return new ArrayList<Question>();
    	}
        return dao.find("select * from t_question where id in ("+ ids +")");
    }
    
    /**
     * 查询课程练习
     * @param courseId
     * @param type
     * @return
     */
    public List<Question> findByCourseId(Long courseId,String type){
        return dao.find("select t.* from t_question t where t.target_id = ? and t.target_type = ? ",courseId,type);
    }
    
    
    /**
     * 分页获取收藏的题目
     * 
     * @date 2016年12月21日15:19:18
     * @param pageNumber
     * @param pageSize
     * @param sql
     * @param paras
     * @return
     */
    public Page<Question> getQuestionCollectionPage(int pageNumber, int pageSize, String sql, Object[] paras) {
		return dao.paginate(pageNumber, pageSize, "select t.* ", sql, paras);
	}
 }
